const express = require('express')
const jwt = require('jsonwebtoken')
const { secretKey } = require('../constant')
const userRouter = express.Router()
const log1 = require('../middleware/log1')
const log2 = require('../middleware/log2')

userRouter.post('/login', (req, res) => {
  const token = jwt.sign(req.body, secretKey, { expiresIn: '30s' })
  res.cc("登录成功",0)
})

userRouter.post('/user/login', (req, res) => {
  req.session.user = req.body
  req.session.isLogin = true
  res.send({ status: '200', msg: '登录成功' })
})
userRouter.post('/user/logout', (req, res) => {
  req.session.destroy()
  res.send({ status: '200', msg: '退出成功' })
})

userRouter.get('/user/info', (req, res) => {
  if (!req.session.isLogin) {
    res.send('401 用户未登录')
  } else {
    res.send(200, req.session.user)
  }
})

userRouter.get('/user/list', (req, res) => {
  console.log('request time ' + req.time)
  res.send('get a user list')
})

userRouter.post('/user/add', (req, res) => {
  res.send('add a user list')
})

userRouter.get('/user', (req, res) => {
  res.send('文本')
})

userRouter.post('/user', [log1, log2], (req, res) => {
  console.log('获取body数据:', req.body)
  console.log('获取body数据:' + JSON.stringify(req.body))
  console.log('获取body数据:' + JSON.stringify(req.body, null, 4))
  console.dir('获取body数据:' + JSON.stringify(req.body, null, 4))
  console.dir(req.body, { depth: null, color: true })
  console.log(req.body)
  res.send({ name: 'zs', age: 35 })
})

userRouter.get('/query', log1, log2, (req, res) => {
  res.send(req.query)
})

userRouter.get('/param/:id/:name', (req, res) => {
  res.send(req.params)
})

module.exports = userRouter
